//
//  UITabBar+RTStyle.m
//  RTUIStyle
//
//  Created by liu lh on 13-5-24.
//  Copyright (c) 2013年 liu lh. All rights reserved.
//

#import "UITabBar+RTStyle.h"
#import "UIComponent.h"

@implementation UITabBar (RTStyle)

- (void)setRtBackgroundColor:(UIColor *)color{
    CGFloat width = ceilf([[UIScreen mainScreen] bounds].size.width/[self.items count]);
    CGSize size = CGSizeMake(width, self.height);
    UIImage *image = [UIImage imageWithColor:color size:size];
    UIImage *backgroundImage = [image resizableImageWithCapInsets:UIEdgeInsetsMake(0, 2, 0, 6)];
    [self rtSetBackgroundImage:backgroundImage];
}

- (void)setRtSelectionIndicatorColor:(UIColor *)color{
    CGFloat width = ceilf([[UIScreen mainScreen] bounds].size.width/[self.items count]);
    CGSize size = CGSizeMake(width, self.height);
    UIImage *image = [UIImage imageWithColor:color size:size];
    UIImage *selectionIndicatorImage = [image resizableImageWithCapInsets:UIEdgeInsetsMake(0, 10, 0, 10)];
    [self setSelectionIndicatorImage:selectionIndicatorImage];
}

- (void)rtSetBackgroundImage:(UIImage *)image{
    if ([self respondsToSelector:@selector(setBackgroundImage:)])
    {
        //ios 5及以后
        [self setBackgroundImage:image];
    }else {
        for (UIView *view in [self subviews]) {
            if ([view isKindOfClass:[UIImageView class]]) {
                [view removeFromSuperview];
            }
        }
        if (image != nil) {
            CGFloat top = MIN(0, 44-image.size.height);
            UIImageView *_imgv = [[UIImageView alloc] initWithImage:image];
            _imgv.frame = CGRectMake(0, top, image.size.width, image.size.height);
            [self insertSubview:_imgv atIndex:0];
        }
    }
}

@end
